package com.sky.common;


import com.sky.entity.SysFile;
import com.sky.exception.ServiceException;
import com.sky.service.ISysFileService;
import com.sky.service.common.IFileService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Log4j2
@Configuration
public class FileCommon
{
    @Resource
    private ISysFileService remoteFileService;

    @Resource
    private IFileService fileService;

    /**
     * 上传文件并保存记录
     *
     * @param file 文件
     * @return [fileIds , filePath]
     */
    public String[] uploadFile(MultipartFile file) {
        // 上传
        SysFile sysFile = null;
        try {
            sysFile = remoteFileService.uploadFile(file);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
        if (Objects.isNull(sysFile)) {
            return new String[0];
        }

        // 保存path
        String relativeUrl = sysFile.getRelativeUrl();

        File file1 = new File();
        file1.setName(file.getName());
        file1.setSize(String.valueOf(file.getSize()));
        file1.setPath(relativeUrl);
        String ids = saveFile(file1);

        if (StringUtils.isBlank(ids)) {
            log.error("查询文件ids失败");
            return new String[0];
        }

        return new String[]{ids, sysFile.getUrl()};
    }

    /**
     * 查询文件
     *
     * @param fileIds 文件id
     * @return 结果
     */
    public List<File> selectFile(String fileIds) {
        if (StringUtils.isBlank(fileIds)) {
            return Collections.emptyList();
        }

        List<File> searchFiles = fileService.selectFiles(fileIds);

        List<File> result = new ArrayList<>(searchFiles.size());
        File file;
        for (File searchFile : searchFiles) {
            file = new File();
            BeanUtils.copyProperties(searchFile, file);
            result.add(file);
        }
        return result;
    }

    /**
     * 查询文件
     *
     * @param fileIds 文件id
     * @return 结果
     */
    public List<File> selectFileUrl(String fileIds) {
        if (StringUtils.isBlank(fileIds)) {
            return Collections.emptyList();
        }

        List<File> searchFiles = fileService.selectFilesUrl(fileIds);

        List<File> result = new ArrayList<>(searchFiles.size());
        File file;
        for (File searchFile : searchFiles) {
            file = new File();
            BeanUtils.copyProperties(searchFile, file);
            result.add(file);
        }
        return result;
    }

    /**
     * 保存path
     *
     * @param fileList 文件path
     * @return 结果
     */
    public String saveFiles(List<File> fileList) {
        if (CollectionUtils.isEmpty(fileList)) {
            return null;
        }

        List<File> list = new ArrayList<>(fileList.size());
       File traFile;
        for (File file : fileList) {
            traFile = new File();
            BeanUtils.copyProperties(file, traFile);
            list.add(traFile);
        }
        return fileService.saveFiles(list);
    }

    /**
     * 文件保存
     *
     * @param file 文件
     * @return 结果
     */
    public String saveFile(File file) {
        Objects.requireNonNull(file);

        File traFile = new File();
        BeanUtils.copyProperties(file, traFile);
        return fileService.saveFile(traFile);
    }

}
